package cqie.frame.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cqie.frame.entity.Clazz;
import cqie.frame.entity.Course;
import cqie.frame.entity.qo.ClazzQo;
import cqie.frame.entity.qo.CourseQo;
import cqie.frame.entity.vo.ClazzVo;
import cqie.frame.entity.vo.CourseVo;
import cqie.frame.response.CodeMsg;
import cqie.frame.response.ResultJson;
import cqie.frame.service.ICourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.thymeleaf.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * (Course)表控制层
 *
 * @author makejava
 * @since 2021-09-27 21:13:19
 */
@RestController
@RequestMapping("/course")
public class CourseController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ICourseService iCourseService;


    //新增课程
    @ApiOperation(value = "新增课程", notes = "新增课程，同时更新课程表。", response = Course.class)
    @PostMapping(value = "/addCourse")
    public ResultJson addCourse(@RequestBody Course po) {
        try {
            //构造mo
            Course course = new Course();
            //复制po的属性到emp,第三个参数可选，为忽略某些属性
            BeanUtils.copyProperties(po, course);
            //插入用户表后回显插入课程的主键id
            iCourseService.save(course);
            po.setId(course.getId());
            //返回课程
            return ResultJson.success(po);
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //删除选中课程，多选
    @ApiOperation(value = "删除选中课程", notes = "删除选中课程。")
    @PostMapping(value = "/deleteCheckCourse")
    public ResultJson deleteCheckCourse(@RequestBody Long[] courseIdList) {
        try {
            for (int i = 0; i < courseIdList.length; i++) {
                iCourseService.removeById(courseIdList[i]);
            }
            return ResultJson.success("删除成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //删除课程
    @ApiOperation(value = "删除单个课程", notes = "删除单个课程。")
    @GetMapping(value = "/deleteCourse")
    public ResultJson deleteCourse(long id) {
        try {
            //删除课程表
            iCourseService.removeById(id);
            //返回前端
            return ResultJson.success("删除成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }


    //修改课程
    @ApiOperation(value = "修改课程", notes = "修改课程，同时更新课程表。")
    @PostMapping(value = "/updateCourse")
    public ResultJson updateCourse(@RequestBody Course po) {
        try {
            //获取修改前的值
            Course course = new Course();
            //复制po的属性到sysUser,第三个参数可选，为忽略某些属性
            BeanUtils.copyProperties(po, course);
            //修改课程
            iCourseService.updateById(course);
            return ResultJson.success("修改成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "分页列表", response = ClazzVo.class)
    @PostMapping(value = "/listPaged")
    public ResultJson listPaged(@RequestBody CourseQo qo) {
        try {
            Page<Course> page = new Page<>(qo.getPageIndex(), qo.getPageSize());
            //1.查询条件
            QueryWrapper<Course> qw = new QueryWrapper<>();
            if (!StringUtils.isEmpty(qo.getName())) {
                //查询模糊查询
                qw.like("name", qo.getName());
            }
            if (!StringUtils.isEmpty(qo.getCode())) {
                //查询模糊查询
                qw.like("code", qo.getCode());
            }
            //是否查询状态
            if (qo.getStatus() != null) {
                //不为空，启用查询
                qw.eq("status", qo.getStatus());
            }
            //2.执行查询
            IPage<Course> datas = iCourseService.page(page, qw);
            //3.生成返回值
            //映射
            List<CourseVo> list = new ArrayList<>();
            IPage<CourseVo> ret = new Page<>();
            BeanUtils.copyProperties(datas, ret);
            for (Course mo : datas.getRecords()) {
                CourseVo vo = new CourseVo();
                BeanUtils.copyProperties(mo, vo);
                //映射--状态
                vo.setStatusName(mo.getStatus() == 0 ? "正常" : "禁用");
                list.add(vo);
            }
            ret.setRecords(list);
            return ResultJson.success(ret);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    @ApiOperation(value = "查看详情", response = CourseVo.class)
    @GetMapping(value = "/details")
    public ResultJson details(Long id) {
        try {
            //1.获取mo
            Course mo = iCourseService.getById(id);
            //2.映射成vo
            CourseVo vo = new CourseVo();
            BeanUtils.copyProperties(mo, vo);
            //映射准备
            //映射--状态
            vo.setStatusName(mo.getStatus() == 0 ? "正常" : "禁用");
            //3.返回
            return ResultJson.success(vo);
        } catch (Exception ex) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //注销/恢复选中课程，多选
    @ApiOperation(value = "注销/恢复选中课程", notes = "注销/恢复选中课程，同时更新课程表。")
    @PostMapping(value = "/disabledCheckCourse")
    public ResultJson disabledCheckCourse(@RequestBody Long[] courseIdList) {
        try {
            for (int i = 0; i < courseIdList.length; i++) {
                Course course = new Course();
                course = iCourseService.getById(courseIdList[i]);
                if (course.getStatus() == 0) {
                    //如果修改前为正常，修改为禁用
                    course.setStatus(1);
                } else if (course.getStatus() == 1) {
                    //如果修改前为禁用，修改为正常
                    course.setStatus(0);
                }
                iCourseService.updateById(course);
            }
            return ResultJson.success("修改成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

    //注销/恢复课程
    @ApiOperation(value = "注销/恢复单个课程", notes = "注销/恢复单个课程，同时更新课程表。")
    @GetMapping(value = "/disabledCourse")
    public ResultJson disabledCourse(long id) {
        try {
            Course course = new Course();
            course = iCourseService.getById(id);
            if (course.getStatus() == 0) {
                //如果状态为正常，修改为禁用
                course.setStatus(1);
            } else if (course.getStatus() == 1) {
                //如果状态为禁用，修改为正常
                course.setStatus(0);
            }
            //修改
            iCourseService.updateById(course);
            return ResultJson.success("操作成功");
        } catch (Exception e) {
            return ResultJson.error(CodeMsg.SERVER_ERROR);
        }
    }

}

